import loader from '@/logic/loader'
import message from '@/logic/message'
import runtimeManager from '@/logic/runtimeManager'

class MessageManager {
  constructor() {
    this.messages = message;
  }

  init(message) {
    this.messages.receive('loadResource',()=>{
      const { appid, bridgeId } = message

      loader.loadResource({
        appid,
        bridgeId,
      })
    })

    this.message.receive('createApp',(msg)=>{
      const {bridgeId,scene,pagePath,query}=msg;
      runtimeManager.createApp({scene,pagePath,query});
      message.send({
        type:'appIsCreated',
        body:{
          bridgeId,
        }
      })
    })

    this.message.receive('appShow',()=>{
      runtimeManager.appShow();
    })
    this.message.receive('appHide',()=>{
      runtimeManager.appHide();
    })
    this.message.receive('pageShow',(msg)=>{
      const {bridgeId}=msg;
      runtimeManager.pageShow({id:bridgeId});
    })
    this.message.receive('pageHide',(msg)=>{
      const {bridgeId}=msg;
      runtimeManager.pageHide({id:bridgeId});
    })
    this.message.receive('moduleMounted',(msg)=>{
      const {id}=msg;
      runtimeManager.pageReady({id:id});
    })
    this.message.receive('pageScroll',(msg)=>{
      const {id,scrollTop}=msg;
      runtimeManager.pageScroll({id:id,scrollTop:scrollTop});
    })
    this.message.receive('createInstance',(msg)=>{
      const {id,path,bridgeId,query}=msg;
      runtimeManager.createPage({id,path,bridgeId,query});
    })
    this.message.receive('makePageInitialData',(msg)=>{
      const {bridgeId,pagePath}=msg;
      const initialData=loader.getInitialDataByPagePath(pagePath)
      message.send({
        type:'initialDataIsReady',
        body:{
          bridgeId,
          initialData
        }
      })
    })
  }


}
export default new MessageManager()